//
//  WeatherView.m
//  快医项目
//
//  Created by Li Ming on 2016/12/20.
//  Copyright © 2016年 xueye. All rights reserved.
//

#import "WeatherView.h"
#import <AFNetworking.h>
#import "WeatherModel.h"
#import "WeatherDetailTableViewController.h"

@interface WeatherView ()
@property (weak, nonatomic) IBOutlet UIImageView *weatherImage;
@property (weak, nonatomic) IBOutlet UILabel *cityLable;
@property (weak, nonatomic) IBOutlet UILabel *tempertureLable;
@property (weak, nonatomic) IBOutlet UILabel *weatherDateLable;
@property (weak, nonatomic) IBOutlet UIButton *cityButton;

@property(strong,nonatomic)NSDictionary *modelDictionary;

@end

@implementation WeatherView
- (IBAction)clickCityButton:(id)sender {
    
    WeatherDetailTableViewController *chooseBigCity = [WeatherDetailTableViewController new];
    
    
    
    NSLog(@"hehheehehhe");
}
-(void)sendButtonWithBlock:(void(^)(UIButton *button))buttonBlock{
    
    if (buttonBlock) {
        buttonBlock(self.cityButton);
    }
    
}


+(instancetype)weatherView{
    
    return [[[NSBundle mainBundle]loadNibNamed:@"Weather" owner:self options:nil]lastObject];
    
}

-(void)awakeFromNib{
    [super awakeFromNib];
    
    [self setupUI];
    
}

-(instancetype)initWithFrame:(CGRect)frame{
    
   self = [super initWithFrame:frame];
    
    if (self) {
        
        [self setupUI];
        
    }
    
    return self;

}

-(void)setupUI{
    
    //建立界面
    
    [self getDataWithCity:@"shanghai"];
    
}

-(void)getDataWithCity:(NSString *)city{
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    AFJSONRequestSerializer *serizlizer = [AFJSONRequestSerializer serializer];
    
    manager.requestSerializer = serizlizer;
    
    //location=<%=ub.getLivecity()%>&output=json&ak=B122767f9cf32ad2c5a17d97835d053e&callback=?
    //NSString *weatherURL = [NSString stringWithFormat:@"http://api.map.baidu.com/telematics/v3/weather?location=上海&output=json&ak=17IvzuqmKrK1cGwNL6VQebF9"];
    
    [manager GET:[NSString stringWithFormat:@"https://api.thinkpage.cn/v3/weather/daily.json?key=qkuqkuxt6flkrw4j&location=%@&language=zh-Hans&unit=c&start=0&days=1",city]parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
        
        NSDictionary *jsonDictionary = responseObject[@"results"][0][@"daily"][0];
        self.modelDictionary = jsonDictionary;
        
        self.cityLable.text = responseObject[@"results"][0][@"location"][@"name"];
        
        WeatherModel *model = [WeatherModel new];
        [model setValuesForKeysWithDictionary:jsonDictionary];
        
        self.model = model;
        
        NSLog(@"%@",self.cityLable.text);
        
        NSLog(@"%@",jsonDictionary);
        
        //转化为模型数组
        
        NSLog(@"Ok");
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"%@",task);
        
        NSLog(@"fail");
        
    }];
    
}
-(void)setModel:(WeatherModel *)model{
    _model = model;
    
    self.tempertureLable.text = [NSString stringWithFormat:@"%@℃- %@℃",model.high,model.low];
    
    self.weatherDateLable.text = model.date;
    
    
}


@end
